﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

public partial class forms_audiorecordings_audiorecordings : System.Web.UI.Page
{
    static StringBuilder _FoldersHtml;
    static object _FoldersHtmlLock = new object();

    Dictionary<string, string> salesPersons = new Dictionary<string, string>();

    static StringBuilder FoldersHtml
    {
        get
        {
            StringBuilder tempBuilder = new StringBuilder();
            int indent = 0;
            foreach (string subFolder in Directory.GetDirectories(HttpContext.Current.Server.MapPath("/fiat/m")))
            {
                Recurse(subFolder, tempBuilder, ref indent);
            }
            return tempBuilder;
        }
    }

    static string ToTurkish(string folderName)
    {
        return folderName
             .Replace("Ý", "İ")
             .Replace("Ð", "Ğ")
             .Replace("Þ", "Ş");
    }

    static void Recurse(string folder, StringBuilder htmlBuilder, ref int indent)
    {
        int fileCount = Directory.GetFiles(folder, "*.wav").Length;
        htmlBuilder.Append("<tr><td colspan='2'>");
        if (fileCount > 0)
        {
            htmlBuilder.AppendFormat("<p style=\"margin-left:{0}0px;\">{1} ({2} Kayıt)&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?f={3}\">Listele</a></p>", indent++, ToTurkish(Path.GetFileName(folder)), fileCount, Convert.ToBase64String(Encoding.UTF8.GetBytes(folder)));
        }
        else
        {
            htmlBuilder.AppendFormat("<p style=\"margin-left:{0}0px;\">{1}</p>", indent++, ToTurkish(Path.GetFileName(folder)));
        }
        htmlBuilder.Append("</td></tr>");
        foreach (string subFolder in Directory.GetDirectories(folder))
        {
            Recurse(subFolder, htmlBuilder, ref indent);
        }
        indent--;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!fiat.isUserLoggedIn())
        {
            Response.Redirect("/");
            return;
        }

        salesPersons.Add("N", "Nilüfer");
        salesPersons.Add("B", "Berrak");
        salesPersons.Add("H", "Hülya");

        if (Request.QueryString["f"] != null)
        {
            string folder = Encoding.ASCII.GetString(Convert.FromBase64String(Request.QueryString["f"]));
            RenderFileView(folder);
        }
        else
        {
            RenderFolderView();
        }
    }

    private void RenderFolderView()
    {
        audioFolderListTable.Text = FoldersHtml.ToString();
    }

    private void RenderFileView(string folder)
    {
        backrow.Visible = true;

        StringBuilder htmlContents = new StringBuilder();
        foreach (string wav in Directory.GetFiles(folder, "*.wav"))
        {
            string fileNameOnly = Path.GetFileNameWithoutExtension(wav);
            string[] fileNameOnlyArray = fileNameOnly.Split('-');
            string salesPerson = fileNameOnlyArray[0].ToUpperInvariant();
            try
            {
                salesPerson = salesPersons[salesPerson];
            }
            catch { }
            htmlContents.AppendFormat
            (
                "<tr><td>Görüşen: {0}, Talep No: {1}, Durum: {2}</td><td>{3} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"{4}\")'><img src='/i/play.gif' border='0' /></a></td></tr>",
                salesPerson,
                fileNameOnlyArray[1],
                fileNameOnlyArray.Length > 2 ? fileNameOnlyArray[2].ToUpperInvariant() == "ST" ? "Satıldı" : fileNameOnlyArray[2].ToUpperInvariant() == "X" ? "İlgilenmiyor" : "Bilinmiyor" : "Bilinmiyor",
                fileNameOnly + ".wav",
                "/" + wav.Replace(Server.MapPath("/"), "").ToLowerInvariant().Replace("/fiat/audiorecordings", "").Replace("\\", "/")
            );
        }
        audioFolderListTable.Text = htmlContents.ToString();
    }

    private void RenderFileView(StringBuilder htmlContents)
    {

    }

}
